
import { createRouter, createWebHistory } from 'vue-router'

// 引入MyHome组件
import MyHome from '@/views/home/MyHome.vue'
import Layout from '@/views/AppLayout.vue'
import Login from '@/views/login/UserLogin.vue'
import App from '@/App.vue'

// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
const routes = [
    {
        path: '/login',
        component: Login,
        meta: { title: "登录" }
    },
    {
        path: '/error',
        component: () => import('@/views/MyError.vue'),
        meta: { title: "错误" }
    },
    {
        path: '/',
        component: Layout,
        meta: { title: "首页" },
        children: [
            // 重定向
            {
                path: '',
                redirect: '/layout'
            },
            // 后台的模板路由
            {
                path: 'layout',
                component: () => import('@/views/layout/MyContainerLayout.vue'),
                meta: { title: "后台首页" },
                name: "layout",
                children: [
                    // 后台路由
                    {
                        // 重定向
                        path: "",
                        redirect: "/layout/index", // 将根路径重定向到 "/home"
                    },
                    // 管理员路由
                    {
                        path: 'index',
                        component: MyHome,
                        name: "homeIndex",
                        meta: { title: "主页面", backgroundColor: "#f6f6f6" }
                    },
                    {
                        path: 'test',
                        component: () => import('@/views/home/MyTest.vue'),
                        name: "test",
                        meta: { title: "测试页面" }
                    },
                    // 作业总览
                    {
                        path: 'homeworksituation',
                        component: () => import('@/views/admin/HomeworkOverview/HomeworkSituation.vue'),
                        name: "homeworksituation",
                        meta: { title: "作业情况" }
                    },
                    {
                        path: 'TeacherReleaseSituation/:type',
                        component: () => import('@/views/admin/HomeworkOverview/TeacherReleaseSituation.vue'),
                        name: "TeacherReleaseSituation",
                        props: true,
                        meta: { title: "老师发布情况" }
                    },
                    // 管理员成绩列表
                    {
                        path: 'achievementslist',
                        component: () => import('@/views/admin/HomeworkOverview/ScoreList.vue'),
                        name: "achievementslist",
                        meta: { title: "成绩列表" }
                    },
                    // 老师路由
                    {
                        path: 'myquestionbank',
                        component: () => import('@/views/teacher/mycourse/MyQuestionBank.vue'),
                        name: "myquestionbank",
                        meta: { title: "我的题库" }
                    },
                    {
                        path: 'MyCourseList',
                        component: () => import('@/views/teacher/mycourse/MyCourseList.vue'),
                        name: "MyCourseList",
                        meta: { title: "我的课程" }
                    },
                    {
                        path: 'myquestionbank',
                        component: App,
                        meta: { title: "我的题库" },
                        children: [
                            {
                                path: 'myquestionbankdetail',
                                component: () => import('@/views/teacher/mycourse/MyQuestionBankDetail.vue'),
                                name: "myquestionbankdetail",
                                meta: { title: "题库详情", activeMenu: '/layout/myquestionbank', backgroundColor: "#f6f6f6" }
                            }
                        ]
                    },
                    {
                        path: 'MyCourseList',
                        component: App,
                        meta: { title: "我的课程" },
                        children: [
                            {
                                path: 'MyCourseDetail',
                                component: () => import('@/views/teacher/mycourse/MyCourseDetail.vue'),
                                name: "MyCourseDetail",
                                meta: { title: "课程详情", keepAlive: true, activeMenu: '/layout/MyCourseList', backgroundColor: "#f6f6f6", }
                            }
                        ]
                    },
                    {
                        path: 'MyCourseList',
                        component: App,
                        meta: { title: "我的课程" },
                        children: [
                            {
                                path: 'MyCourseDetail',
                                component: App,
                                name: "MyCourseDetail1",
                                meta: { title: "课程详情", activeMenu: '/layout/MyCourseList', backgroundColor: "#f6f6f6" },
                                children: [
                                    {
                                        path: 'PreviewHomework/:id/:type/:wid?',
                                        props: true,
                                        component: () => import('@/components/PreviewHomework.vue'),
                                        name: "PreviewHomework",
                                        meta: { title: "作业预览", activeMenu: '/layout/MyCourseList', backgroundColor: "#f6f6f6" },
                                    }
                                ]
                            }
                        ]
                    },

                    // 学生成绩
                    {
                        path: 'studentachievements',
                        component: () => import('@/views/teacher/mycourse/StudentAchievements.vue'),
                        name: "studentachievements",
                        meta: { title: "学生成绩", backgroundColor: "#f6f6f6" }
                    },

                    // 学生提问
                    {
                        path: 'StudentQuestion',
                        component: () => import('@/views/teacher/mycourse/StudentQuestion.vue'),
                        name: "StudentQuestion",
                        meta: { title: "学生提问", backgroundColor: "#f6f6f6" }
                    },

                    // 学生路由

                    // 课程列表
                    {
                        path: 'courselist',
                        component: () => import('@/views/student/StudentCourseList.vue'),
                        name: "courselist",
                        meta: { title: "课程列表", }
                    },
                    // 课程详情
                    {
                        path: 'courselist',
                        component: App,
                        meta: { title: "课程列表" },
                        children: [
                            {
                                path: 'MyHomeworkDetail',
                                component: () => import('@/views/student/MyHomeworkDetail.vue'),
                                name: "MyHomeworkDetail",
                                meta: { title: "作业列表", activeMenu: '/layout/courselist', }
                            }
                        ]
                    },
                    // 作业预览
                    {
                        path: 'courselist',
                        component: App,
                        meta: { title: "课程列表" },
                        children: [
                            {
                                path: 'MyHomeworkDetail',
                                component: App,
                                name: "MyHomeworkDetail1",
                                meta: { title: "作业列表", activeMenu: '/layout/courselist', backgroundColor: "#f6f6f6" },
                                children: [
                                    {
                                        // type 1 预览/答题 2会查看答案
                                        path: 'PreviewHomework/:id/:type/:wid?',
                                        props: true,
                                        component: () => import('@/components/PreviewHomework.vue'),
                                        name: "PreviewHomework2",
                                        meta: { title: "作业预览", activeMenu: '/layout/courselist', backgroundColor: "#f6f6f6" },
                                    }
                                ]
                            }
                        ]
                    },
                    // 错题本
                    {
                        path: 'WrongQuestionBook',
                        component: () => import('@/views/student/WrongQuestionBook.vue'),
                        name: "WrongQuestionBook",
                        meta: { title: "错题本", }
                    },
                    // 学生成绩
                    {
                        path: 'StudentScore',
                        component: () => import('@/views/student/StudentAchievements.vue'),
                        name: "StudentScore",
                        meta: { title: "学生成绩", backgroundColor: "#f6f6f6" }
                    },
                    // 后台个人中心路由
                    {
                        path: 'userinfo',
                        component: App,
                        name: "userinfo",
                        meta: { title: "用户管理" },
                        children: [
                            {
                                // 重定向
                                path: "",
                                redirect: "/layout/userinfo/index", // 将根路径重定向到 "/home"
                            },
                            {
                                path: 'error',
                                component: () => import('@/views/MyError.vue'),
                                meta: { title: "错误页" }
                            },

                            {
                                path: 'index',
                                component: () => import('@/views/admin/user/MyUserInfo.vue'),
                                name: "userinfo",
                                meta: { title: "用户信息", backgroundColor: "#f6f6f6" }
                            },
                            {
                                path: 'loginrecord',
                                component: () => import('@/views/admin/user/LoginRecords.vue'),
                                name: "login_record",
                                meta: { title: "登录记录", backgroundColor: "#f6f6f6" }
                            },
                            {
                                path: 'updata_password',
                                component: () => import('@/views/admin/user/MyUserInfoUpdataPassword.vue'),
                                name: "updata_password",
                                meta: { title: "修改密码", }
                            },
                            {
                                path: 'management_user',
                                component: () => import('@/views/admin/user/MyUserInfoList.vue'),
                                name: "management_user",
                                meta: { title: "用户列表", }
                            },
                        ]
                    }
                ]
            },
            // 非后台模板路由
            {
                path: 'nolayout',
                component: () => import('@/views/layout/MyContainerNoLayout.vue'),
                name: "nolayout",
                children: [
                    {
                        // 重定向
                        path: "",
                        redirect: "/nolayout/home", // 将根路径重定向到 "/home"
                    },
                    {
                        path: 'home',
                        component: () => import('@/views/home/MyNoHome.vue'),
                        meta: { title: "首页", backgroundColor: "#E9E9E9" }
                    }
                ]
            }
        ]
    }
]
// 3. 创建路由实例并传递 `routes` 配置
const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
    console.log(to);
    // 判断是否需要登录
    if (to.path != '/login') {
        // 判断是否已经登录
        if (sessionStorage.getItem('token')) {
            next();
        } else {
            next({
                path: '/login',
                query: {
                    redirect: to.fullPath
                }
            })
        }
    } else {
        next();
    }
})

export default router